package com.i2works.smartluolong.manage.service.admin.impl;

import com.i2works.smartluolong.manage.dal.admin.dao.IAdminMenuDAO;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuDO;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminMenuService;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统菜单表实现类
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminMenuServiceImpl
 * @date 2016年01月21日 下午10:48:29
 */
@Service("adminMenuService")
public class AdminMenuServiceImpl extends BaseCommonServiceImpl<AdminMenuQuery, AdminMenuDO> implements IAdminMenuService {

    @Autowired
    private IAdminMenuDAO adminMenuDAO;


    @Override
    public List<AdminMenuDO> queryAllList(AdminMenuQuery query) throws ServiceException {
        try {
            return adminMenuDAO.queryAllList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void save(AdminMenuDO adminMenuDO) throws ServiceException {
        try {
            if (adminMenuDO.getId() != null && adminMenuDO.getId() != 0) {
                adminMenuDAO.update(adminMenuDO);
            } else {
                adminMenuDO.setCreateName(adminMenuDO.getUpdateName());
                adminMenuDAO.save(adminMenuDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void updateStatus(AdminMenuDO adminMenuDO) throws ServiceException {
        try {
            if (null != adminMenuDO) {
                adminMenuDAO.update(adminMenuDO);
                //如果是父类，关联设置其子类状态
                if (adminMenuDO.getPid() == 0) {
                    AdminMenuQuery query = new AdminMenuQuery();
                    query.setPid(adminMenuDO.getId());
                    //查询其所有子类
                    List<AdminMenuDO> childrenResList = adminMenuDAO.queryAllList(query);
                    //循环输出子类，并设置其status为父类status
                    for (AdminMenuDO childrenMenu : childrenResList) {
                        childrenMenu.setStatus(adminMenuDO.getStatus());
                        //更新子类
                        adminMenuDAO.update(childrenMenu);
                    }
                }
            }

        } catch (DAOException e) {
            throw new ServiceException(e);
        }

    }

}
